package com.sjl.controller;

import cn.hutool.log.StaticLog;
import com.alibaba.dubbo.config.annotation.Reference;
import com.sjl.springboot.domain.CommonResponse;
import com.sjl.springboot.domain.enums.CommonCode;
import com.sjl.springboot.domain.page.PageQuery;
import com.sjl.springboot.domain.user.SysUser;
import com.sjl.springboot.inface.user.UserService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户中心控制类
 * @author sjl
 * @date 2018年5月29日 11:17:42
 */
@RestController
@RequestMapping("/user")
@Api("UserController相关的api")
public class UserController {

    @Reference
    private UserService userService;

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public Object userList() {
        List<SysUser> userList = userService.findAllUsers();
        SysUser user = new SysUser();
        StaticLog.info(userList.toString());
        return userList;
    }
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public Object user() {
        PageQuery<SysUser> pageQuery = new PageQuery<SysUser>();
        pageQuery.setPageNum(1);
        pageQuery.setSize(10);
        CommonResponse<SysUser> response = userService.findPageUsers(pageQuery);
        return response;
    }

    @RequestMapping(value = "/update/{id:\\d+}", method = RequestMethod.POST)
    public Object updateUser( @PathVariable("id")String id){
        StaticLog.info("id{}",id);
        Object  result=null;
        CommonResponse<SysUser> res = userService.findUserById(id);
        if(StringUtils.equals(res.getCode(), CommonCode.Success_msg.getCode())){

            CommonResponse response = userService.updateUser(res.getResult());
            result = response;
        }else{
            result =res;
        }


        return result;
    }

}
